<template>
    <div class="header">
    <div class="nav-topbar">
      <div class="container">
        <div class="topbar-menu">
          <a href="javascript:;">团结</a>
          <a href="javascript:;">勤奋</a>
          <a href="javascript:;">求实</a>
          <a href="javascript:;">创新</a>
        </div>
        <div class="topbar-user">
             <a href="javascript:;" v-if="username">{{username}}</a>
          <a href="javascript:;" v-if="!username" @click="login">登录</a>
          <a href="javascript:;" v-if="username" @click="logout">退出</a>
          <a href="/#/order/list" v-if="username">我的订单</a>
          <a href="javascript:;" class="my-cart" @click="goToCart()">收藏夹({{count}})</a>
           <!-- <a href="javascript:;" class="my-cart"><span class="icon-cart">收藏夹</span></a> -->
        </div>
      </div>
    </div>
    <div class="nav-header">
      <div class="container">
        <!-- logo -->
        <div class="header-logo">
          <a href="/#/index"></a>
        </div>
        <!-- 菜单 -->
        <div class="header-menu">
        <div class="item-menu">
          <span>全新商品</span>
          <div class="children">
            <ul>
              <li class="product" v-for="(item,index) in phoneList" :key="index">
                  <a v-bind:href="'/#/goods/'+ item.id" target="_blank">
                    <div class="pro-img">
                       <img :src="item.subImages" :alt="item.name">
                    </div>
                    <div class="pro-name">{{item.name}}</div>
                    <div class="pro-price">{{item.price}}元</div>
                  </a>
              </li>
            </ul>
          </div>
        </div>
         <div class="item-menu">
          <span>二手商品</span>
          <div class="children">
            <ul>
              <li class="product" v-for="(item,index) in oldList" :key="index">
                  <a v-bind:href="'/#/goods/'+ item.id" target="_blank">
                    <div class="pro-img">
                       <img :src="item.subImages" :alt="item.name">
                    </div>
                    <div class="pro-name">{{item.name}}</div>
                    <div class="pro-price">{{item.price}}元</div>
                  </a>
              </li>
            </ul>
          </div>
        </div>
         <div class="item-menu">
          <span>哈哈</span>
          <div class="children"></div>
        </div>
        </div>
        <div class="header-search">
          <div class="wrapper">
           <input type="text" name="keyword">
            <a href="javascript:;"></a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import axios from 'axios'
  export default{
    name:'nav-header',  
    data(){
      return{
        username:'',
        count:0,
        phoneList:[],
        oldList:[]
      }
    },
    created(){
       this.getUser();
       this.getCount();
    },
    mounted(){
      //全新商品
      this.getProductList();
      this.getoldGoods();
    },
    methods:{
      getProductList(){
        axios.get('goods/status',{
          params:{
            status:'4'
          }
        }).then((res)=>{
          this.phoneList = res;
        })
      },
      getoldGoods(){
         axios.get('goods/status',{
          params:{
            status:'5'
          }
        }).then((res)=>{
          this.oldList = res;
        })
      },
       getUser(){
      axios.get('/user/userinfo').then(res=>{
           this.username = res.nickname    
      })
    },
      getCount(){
      axios.get('/collection/count').then(res=>{
           this.count = res
           console.log(this.count)    
      })
      },
      goToCart(){
        this.$router.push('/cart');
      },
      login(){
        this.$router.push('/login');
      },
      logout(){
        localStorage.removeItem('token');
        this.$router.push('/login');
      }
    }
  }
</script>

<style lang="scss">
.header{
  .nav-topbar{
    height:39px;
    line-height:39px;
    background-color:#333333;
    color:#B0B0B0;
      .container{
        width: 1226px;
        margin-right: auto;
        margin-left: auto;
        display: flex;
        justify-content: space-between;
        // align-items: center;
        a{
          display:inline-block;
          color:#B0B0B0;
          margin-right:17px;
        }
        .my-cart{
          width:110px;
          background-color:#FF6600;
          text-align:center;
          color:#ffffff;
          // margin-right: 0;
          .icon-cart{
            display:inline-block;
            height:12px;
            background:url('/imgs/icon-cart-checked.png') no-repeat center;
            background-size:contain;
            margin-right:4px;
          }
        }
       }
  }
  .nav-header{
    .container{
      margin-top: 20px;
        position: relative;
        display: flex;
        justify-content: space-between;
        align-content: center;
        width: 1226px;
        height:112px;
        margin-right:auto;
        margin-left:auto;
        display:flex;
        justify-content:space-between;
        .header-logo{
          display:inline-block;
          width:55px;
          height:55px;
          a{   
            display:inline-block;
            width:55px;
            height:55px;
            background-color:#FF6600;
            &:before{
              content:' ';
              display:inline-block;
              width:55px;
              height:55px;
              background:url('/imgs/mi-logo.png') no-repeat center;
              background-size:55px;
              transition: margin .2s;
            }
            &:after{
              content:'';
              display:inline-block;
              width:55px;
              height:55px;
              background:url('/imgs/mi-home.png') no-repeat center;
              background-size:55px;
            }
            &:hover:before{
              top:28px;
              right:0;
              margin-left:-55px;
              transition: margin .2s;
            }
          }
        }
        .header-menu{
          display: inline-block;
          width: 643px;
          padding-left: 209px;
          .item-menu{
            margin-left: 20px;
            margin-right: 20px;
            margin-top: -20px;
            display: inline-block;
            color: #333333;
            font-weight: bold;
            font-size: 16px;
            line-height: 112px;


            span{
              cursor: pointer;
            }
            &:hover{
             color: #FF6600;
             .children{
               height: 220px;
               opacity: 1;
             }
            }
            .children{
              position: absolute;
              top: 112px;
              left: 0;
              width: 1226px;
              height: 0;
              opacity: 0; //透明度
              overflow: hidden;
              border-top: 1px solid #e5e5e5;
              box-shadow: 0px 7px 6px 0px rgba(0,0,0,0.11);
              z-index:10;
              transition: all 0.5s;
              background-color: #ffffff;
              .product{
                position:relative; 
                float: left;
                width:16.6%;
                height: 220px;
                font-size: 12px;
                line-height: 12PX;
                text-align: center;
                a{
                  display: inline-block;
                }
                img{
                  width: auto;
                  height: 111px;
                  margin-top: 26px;
                }
                .pro-img{
                  height: 137px;

                }
                .pro-name{
                  font-weight: bold;
                  margin-top: 19px;
                  margin-bottom: 8px;
                  color: #333333;
                }
                .pro-price{
                  color: #ff6600;
                }
                &:before{
                content: ' ';
                position: absolute;
                top: 28px;
                right: 0;
                border-left: 1px solid #d7d7d7;
                height: 100px;
                width: 1px;
              }
              &:last-child::before{
                display: none;
              }
              }

            }
          }
        }
        .header-search{
          width:319px;
          .wrapper{
            height:50px;
            border:1px solid #E0E0E0;
            display:flex;
            align-items:center;
            input{
              border:none;
              box-sizing: border-box;
              border-right:1px solid #E0E0E0;
              width:264px;
              height:50px;
              padding-left:14px;
            }
            a{
              display: inline-block;
              width: 18px;
              height: 18px;
              background:url('/imgs/icon-search.png') no-repeat center;
              background-size: contain;
              margin-left: 19px;
            }
          }

        }
    }
  }
}
</style>